import '../public/index'
import url from '../data/url'
import request from '../data/request'
import {tableConfig, header} from '../data/config'
import {getUrlParam, fetch2} from '../data/util'
import store from 'store'

const type = getUrlParam('type')

const base = url.base
let table, storeData

const app = {
  examinationType: type,
  apiUrl: 'admin/applyRecord/findApplyHistory.json',
  iSendColumns: [
    {
      field: 'requestUserName',
      title: '发起人',
      valign: 'middle'
    }, {
      field: 'approveTitle',  // 接口没传终端号
      title: '审批标题',
      valign: 'middle'
    }, {
      field: 'approveSummary',
      title: '审批摘要',
      valign: 'middle'
    }, {
      field: 'rawAddTime',
      title: '发起时间',
      valign: 'middle',
      formatter: function (index, row) {
        if (row.rawAddTime) {
          const time = new Date(row.rawAddTime)
          return time.toLocaleDateString() + time.toLocaleTimeString()
        }
      }
    }, {
      field: 'finishTime',
      title: '完成时间',
      valign: 'middle',
      formatter: function (index, row) {
        if (row.finishTime) {
          const time = new Date(row.finishTime)
          return time.toLocaleDateString() + time.toLocaleTimeString()
        }
      }
    },
    {
      field: 'status',
      title: '状态',
      valign: 'middle',
      formatter: function (index, row) {
        let staStr
        switch (row.status) {
          case 0:
            staStr = '未提交'
            break
          case 1:
            staStr = '审批中'
            break
          case 2:
            staStr = '已通过'
            break
          case 3:
            staStr = '不通过'
            break
          case 4:
            staStr = '已驳回'
            break
          case 5:
            staStr = '已撤销'
            break
        }
        return staStr
      }
    }, {
      field: 'control',
      title: '操作',
      valign: 'middle',
      width: '80px',
      formatter: function (index, row) {
        switch (row.status) {
          case 1:
            return `<div class="btn-group">
                    <button type="button" data-identifier="${ row.identifier }" data-id="${ row.id }" class="btn btn-default btn-modify go-info do-not-send">撤回</button>
                  </div>`
            break
        }
      }
    }
  ],
  needIColumns: [
    {
      field: 'requestUserName',
      title: '发起人',
      valign: 'middle'
    }, {
      field: 'approveTitle',  // 接口没传终端号
      title: '审批标题',
      valign: 'middle'
    }, {
      field: 'approveSummary',
      title: '审批摘要',
      valign: 'middle'
    }, {
      field: 'rawAddTime',
      title: '发起时间',
      valign: 'middle',
      formatter: function (index, row) {
        if (row.rawAddTime) {
          const time = new Date(row.rawAddTime)
          return time.toLocaleDateString() + time.toLocaleTimeString()
        }
      }
    }, {
      field: 'control',
      title: '操作',
      valign: 'middle',
      width: '80px',
      formatter: function (index, row) {
        return `<div class="btn-group">
                    <button type="button" data-identifier="${ row.identifier }" data-id="${ row.id }" class="btn btn-default btn-modify go-info agree">同意</button>
                    <button type="button" data-identifier="${ row.identifier }" data-id="${ row.id }" class="btn btn-default btn-modify go-info do-not-agree">拒绝</button>
                  </div>`
      }
    }
  ],
  overColumns: [
    {
      field: 'requestUserName',
      title: '发起人',
      valign: 'middle'
    }, {
      field: 'approveTitle',  // 接口没传终端号
      title: '审批标题',
      valign: 'middle'
    }, {
      field: 'approveSummary',
      title: '审批摘要',
      valign: 'middle'
    }, {
      field: 'rawAddTime',
      title: '发起时间',
      valign: 'middle',
      formatter: function (index, row) {
        if (row.rawAddTime) {
          const time = new Date(row.rawAddTime)
          return time.toLocaleDateString() + time.toLocaleTimeString()
        }
      }
    }, {
      field: 'status',
      title: '操作',
      valign: 'middle',
      width: '80px',
      formatter: function (index, row) {
        let staStr
        switch (row.result) {
          case 0:
            staStr = '未提交'
            break
          case 1:
            staStr = '审批中'
            break
          case 2:
            staStr = '已通过'
            break
          case 3:
            staStr = '不通过'
            break
          case 4:
            staStr = '已驳回'
            break
          case 5:
            staStr = '已撤销'
            break
        }
        return staStr
      }

    }
  ],
  init() {
    this.initTable()
    this.bindEvent()
    this.bindStyle()
  },
  initTable() {
    let columns
    if (this.examinationType === 'iSend') {
      this.apiUrl = 'admin/applyRecord/findApplyHistory.json'
      columns = this.iSendColumns
    }
    if (this.examinationType === 'needI') {
      this.apiUrl = 'admin/applyRecord/findTodoApply.json'
      columns = this.needIColumns
    }

    if (this.examinationType === 'overSend') {
      this.apiUrl = 'admin/approveRecord/findApproveHistory.json'
      columns = this.overColumns
    }

    table = $('#table').bootstrapTable($.extend({}, tableConfig, {
      url: base + this.apiUrl,
      queryParams: function (params) {
        return $.extend({
          currentPage: params.pageNumber,
          pageSize: params.pageSize,
        }, $('#searchForm').serializeObject())
      },
      ajax: function (params) {  //  自定义ajax
        $.ajax({
          url: params.url,
          type: params.type,
          contentType: params.contentType,
          headers: {
            myToken: Cookies.get('myToken')
          },
          data: params.data
        })
          .done(function (res) {

            console.log(res)
            console.log("table success")

            params.success({
              total: res.object.totalRows,
              rows: res.object instanceof Array ? res.object : res.object.elements
            })

          })
          .fail(function (res) {
            console.log('error====>>', res)
          })

      },
      columns: columns
    }));
  },
  bindEvent() {
    table.on('click-row.bs.table', (e, data, el) => {
      storeData = data
      store.set('storeData', data)
      // console.log(data)
    });

    $(document).on('click', '.examination-nav-head', e => {
      const type = e.target.id
      location.href = `./index.html?type=${type}`
    })

    $(document).on('click', '#search-btn', e => {
      e.preventDefault()
      table.bootstrapTable('refresh', {
        query: $.extend({
          currentPage: 1,
        }, $('#searchForm').serializeObject())
      })
    })

    // 这个页面操作的类型
    const arr = ['do-not-send', 'do-not-agree', 'agree']
    // 传到审批接口的信息
    const data = {id: null, businessId: null, approveTitle: null, result: null, status: null}
    // 表格操作按钮绑定
    arr.map(k => {
      $(document).on('click', `.${k}`, e => {
        let result
        const btn = $(e.target).text()
        switch (btn) {
          case'撤回':
            result = 5
            break
          case'同意':
            result = 2
            break
          case'拒绝':
            result = 3
            break
        }

        const storeData = store.get('storeData')
        data.id = storeData.idStr
        data.businessId = storeData.businessIdStr
        data.approveTitle = storeData.approveTitle
        data.result = result
        data.status = storeData.status
        console.log('data===>>>', data)

        const examinationLayer = layer.confirm('', {
          content: `请确认是否${btn}`,
          btn: ['是', '否'] //按钮
        }, function () {
          fetch2('admin/applyRecord/commitApply.json', data).then(res => {
            if (res.code == 0) {
              window.location.reload()
            }
          })
        }, function () {
          layer.close(examinationLayer)
        })
      })
    })
  },
  // 绑定tab栏样式
  bindStyle() {
    $(`#${this.examinationType}`).addClass('active')
  }
}
app.init()


